package com.elm.service.impl;

import com.elm.mapper.CartMapper;
import com.elm.po.Cart;
import com.elm.service.CartService;
import com.elm.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private HttpServletRequest request;

    private String getCurrentUserId() {
        String token = (String) request.getAttribute("token");
        return JwtUtil.getUserIdFromToken(token);
    }

    @Override
    public int saveCart(Cart cart) {
        cart.setUserId(getCurrentUserId());
        return cartMapper.saveCart(cart);
    }

    @Override
    public int updateCart(Cart cart) {
        cart.setUserId(getCurrentUserId());
        return cartMapper.updateCart(cart);
    }

    @Override
    public int removeCart(Cart cart) {
        cart.setUserId(getCurrentUserId());
        return cartMapper.removeCart(cart);
    }

    @Override
    public List<Cart> getCart(Cart cart) {
        cart.setUserId(getCurrentUserId());
        return cartMapper.getCart(cart);
    }
}